// 生成随机数
const randomNumber = function (length) {
  var data = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L']
  var result = ''
  for (var i = 0; i < length; i++) {
    var number = Math.floor(Math.random() * 28)
    result += data[number]
  }
  return result
}
const timestampToTime = function (timestamp) {
  var date = new Date(timestamp)
  var year = date.getFullYear() + '-'
  var month =
    (date.getMonth() + 1 < 10 ?
      '0' + (date.getMonth() + 1) :
      date.getMonth() + 1) + '-'
  var day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
  var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
  var currentdate = year + month + day + h + m + s
  return currentdate
}
const nothmsTimestampToTime = function (timestamp) {
  var date = new Date(timestamp)
  var year = date.getFullYear() + '-'
  var month =
    (date.getMonth() + 1 < 10 ?
      '0' + (date.getMonth() + 1) :
      date.getMonth() + 1) + '-'
  var day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
  var currentdate = year + month + day
  return currentdate
}
// 组装数据
const convertToTreeData = function (data, pid) {
  const result = []
  let temp = []
  if (typeof(pid)=== "number") { 
    pid = pid +''
  }
  for (let i = 0; i < data.length; i++) {
    if (typeof(data[i].pid)  === "number" ) { 
      data[i].pid = data[i].pid +''
    }
    if (data[i].pid === pid) {
      const obj = {
        'label': data[i].label,
        'value': typeof(data[i].value) === "number" ?(data[i].value+''):data[i].value
      }
      temp = this.convertToTreeData(data, data[i].value)
      if (temp.length > 0) {
        obj.children = temp
      }
      result.push(obj)
    }
  }
  return result
}
// 下拉数据去重
const arrayUnique = function (arr, name) {
  var hash = {};
  return arr.reduce(function (item, next) {
    hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
    return item;
  }, []);
}
// 深拷贝
const deepClone = function (obj) {
  let objClone = Array.isArray(obj) ? [] : {};
  if (obj && typeof obj === "object") {
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        //判断ojb子元素是否为对象，如果是，递归复制
        if (obj[key] && typeof obj[key] === "object") {
          objClone[key] = this.deepClone(obj[key])
        } else {
          //如果不是，简单复制
          objClone[key] = obj[key]
        }
      }
    }
  }
  return objClone;
}
export default {
  randomNumber,
  timestampToTime,
  nothmsTimestampToTime,
  convertToTreeData,
  arrayUnique,
  deepClone
}
